<?php

/**
 * @application Automatical Judging System
 * @author deltamaster
 * @copyright Softrank Research Center
 * @version 2.0.0.0
 */

require_once "./include/common.inc.php";

if ($adminid != 1)
	showmessage($lang['invalid_operation'], 'index.php');
/*
if (submitcheck('calculate'))
{
if ($adminid != 1) showmessage($lang['invalid_operation'], 'index.php');
$query = $db->query("SELECT `uid` FROM `sr_user`");
for ($i = 0; $i < $db->num_rows($query); $i++) $uidcollection[$i] = $db->fetch_array($query);
foreach ($uidcollection as $uid)
{
$score = CalculateUserScore($uid['uid']);
$updatequery = $db->query("UPDATE `sr_user` SET `score`='{$score}' WHERE `uid`='{$uid['uid']}'");
}
showmessage($lang['update_score_success'], 'admin.php');
}


*/
if ($action == 'add')
{
	if (submitcheck('add'))
	{
		$problemset = array();
		$problemset['problem'] = $problem;
		switch ($ptype)
		{
			case '1':
				//$manual = 0;
				$problemset['answer'] = $blankanswer;
				$problemset['judge'] = (trim($blankjudge) == '' ? "^\\s*{$blankanswer}\\s*\$" :
					$blankjudge);
				break;
			case '2':
				$manual = 0;
				$tempproblemset['choice'] = explode("\n", $choice);
				$m = 1;
				foreach ($tempproblemset['choice'] as $key => $option)
				{
					$problemset['choice'][$key]['string'] = $tempproblemset['choice'][$key];
					$problemset['choice'][$key]['value'] = $m;
					$m *= 2;
				}
				$problemset['answer'] = $choiceanswer;
				$problemset['judge'] = "^{$choiceanswer}\$";
				break;
			case '3':
				$manual = 0;
				$tempproblemset['mchoice'] = explode("\n", $mchoice);
				$m = 1;
				foreach ($tempproblemset['mchoice'] as $key => $option)
				{
					$problemset['choice'][$key]['string'] = $tempproblemset['mchoice'][$key];
					$problemset['choice'][$key]['value'] = $m;
					$m *= 2;
				}
				$problemset['answer'] = 0;
				$tempproblemset['answer'] = $mchoiceanswer;
				foreach ($tempproblemset['answer'] as $key => $value)
				{
					$problemset['answer'] += $value;
				}
				$problemset['judge'] = "^{$problemset['answer']}\$";
				break;
			case '4':
				$manual = 1;
				$problemset['answer'] = $essayanswer;
				$problemset['judge'] = "";
				break;
		}
		//print_r($problemset);
		$problemstring = base64_encode(serialize($problemset));

		if ($db->query("insert into `{$tablepre}problem` (`type`, `problem`, `manual`, `score`, `level`) values ('{$ptype}', '{$problemstring}', '{$manual}', '{$score}', '{$level}')"))
		{
			showmessage($lang['add_success'], 'admin.php?action=add');
		}
		else
		{
			showmessage($lang['add_fail'], 'admin.php?action=add');
		}
	}
}

if ($action == 'edit')
{
	if (empty($_GET['pid']))
		showmessage($lang['invalid_operation'], 'index.php'); //Without a pid
	$query = $db->query("SELECT * FROM `{$tablepre}problem` WHERE `id`='{$_GET['pid']}'");
	if (!$db->num_rows($query))
		showmessage($lang['invalid_operation'], 'index.php'); //pid not found
	$problem = $db->fetch_array($query);
	$problem['problem'] = unserialize(base64_decode($problem['problem']));
	//print_r($problem);

	if (submitcheck('edit'))
	{
		if (!VerifyScore($score))
			showmessage($lang['edit_fail'], "admin.php?action=edit&pid={$pid}");
		$problemset = array();
		$problemset['problem'] = $problem;
		switch ($ptype)
		{
			case '1':
				//$manual = 0;
				$problemset['answer'] = $blankanswer;
				$problemset['judge'] = (trim($blankjudge) == '' ? "^\\s*{$blankanswer}\\s*\$" :
					$blankjudge);
				break;
			case '2':
				$manual = 0;
				$tempproblemset['choice'] = explode("\n", $choice);
				$m = 1;
				foreach ($tempproblemset['choice'] as $key => $option)
				{
					$problemset['choice'][$key]['string'] = $tempproblemset['choice'][$key];
					$problemset['choice'][$key]['value'] = $m;
					$m *= 2;
				}
				$problemset['answer'] = $choiceanswer;
				$problemset['judge'] = "^{$choiceanswer}\$";
				break;
			case '3':
				$manual = 0;
				$tempproblemset['mchoice'] = explode("\n", $mchoice);
				$m = 1;
				foreach ($tempproblemset['mchoice'] as $key => $option)
				{
					$problemset['choice'][$key]['string'] = $tempproblemset['mchoice'][$key];
					$problemset['choice'][$key]['value'] = $m;
					$m *= 2;
				}
				$problemset['answer'] = 0;
				$tempproblemset['answer'] = $mchoiceanswer;
				foreach ($tempproblemset['answer'] as $key => $value)
				{
					$problemset['answer'] += $value;
				}
				$problemset['judge'] = "^{$problemset['answer']}\$";
				break;
			case '4':
				$manual = 1;
				$problemset['answer'] = $essayanswer;
				$problemset['judge'] = "";
				break;
		}
		//print_r($problemset);
		$problemstring = base64_encode(serialize($problemset));
		if ($db->query("update `{$tablepre}problem` set `type`='{$ptype}', `problem`='{$problemstring}', `manual`='{$manual}', `score`='{$score}', `level`='{$level}' where `id`='{$pid}'"))
		{
			showmessage($lang['edit_success'], "admin.php?action=edit&pid={$pid}");
		}
		else
		{
			showmessage($lang['edit_fail'], 'admin.php?action=edit');
		}
		
	}
}

if ($action == 'delete')
{
	if (empty($_GET['pid']))
		showmessage($lang['invalid_operation'], 'index.php'); //Without a pid
	$query = $db->query("SELECT * FROM `{$tablepre}problem` WHERE `id`='{$_GET['pid']}'");
	if (!$db->num_rows($query))
		showmessage($lang['invalid_operation'], 'index.php'); //pid not found
	$query = $db->query("delete from `{$tablepre}problem` where `id`='{$_GET['pid']}'");
	showmessage($lang['delete_success'], "admin.php?action=list");
}

if ($action == 'list')
{
	$query = $db->query("SELECT * FROM `{$tablepre}problem` ORDER BY `id` ASC");
	while ($tempproblem = $db->fetch_array($query))
	{
		$problem[$tempproblem['id']] = $tempproblem;
		$problem[$tempproblem['id']]['problem'] = unserialize(base64_decode($tempproblem['problem']));
	}
}

if ($action == 'judge')
{
	if (submitcheck('judge'))
	{
		$query = $db->query("SELECT * FROM `{$tablepre}view_submit` WHERE `id`='{$rid}'");
		if ($db->num_rows($query))
		{
			$record = $db->fetch_array($query);
			if ($judgescore > $record['score'] || $judgescore < 0)
			{
				showmessage($lang['invalid_score'], 'admin.php?action=judge');
			}
			else
			{
				$db->query("UPDATE `{$tablepre}record` SET `type`='judge', `event`='{$judgescore}' WHERE `id`='{$rid}'");
				showmessage($lang['judge_success'], 'admin.php?action=judge');
			}
		}
		else
			showmessage($lang['invalid_operation']);
	}
	$recordset = array();
	$query = $db->query("SELECT * FROM `{$tablepre}view_submit`");
	while ($temprecord = $db->fetch_array($query))
	{
		$recordset[$temprecord['id']] = $temprecord;
		$recordset[$temprecord['id']]['problem'] = unserialize(base64_decode($temprecord['problem']));
	}
}

if (submitcheck('settings'))
{
	if ($adminid != 1)
		showmessage($lang['invalid_operation'], 'index.php');
	$inputstarttime = ProcessTime($_POST['config']['starttime']);
	$inputendtime = ProcessTime($_POST['config']['endtime']);
	if (!$inputstarttime || !$inputendtime || $inputstarttime['time'] > $inputendtime['time'])
		showmessage($lang['end_before_start'], 'admin.php?action=settings');
	if (!VerifyContestName($_POST['config']['contestname']))
		showmessage($lang['configure_invalid'], 'admin.php?action=settings');

	foreach ($_POST['config'] as $configname => $configvalue)
	{
		$query = $db->query("UPDATE `{$tablepre}config` SET `value`='{$configvalue}' WHERE `name`='{$configname}'");
	}
	showmessage($lang['configure_success'], 'admin.php');
}

if ($action == 'announce')
{
	if (submitcheck('submitannouncement'))
	{
		if (!$submituid) $submituid = 0;
		$query = $db->query("insert into `{$tablepre}record` (`uid`, `pid`, `type`, `event`) values ('{$submituid}', '0', '{$announcementtype}', '{$announcement}')");
	}
}

if ($action == 'userlist')
{
	$query = $db->query("select * from `{$tablepre}user`");
	$userlist = array();
	while ($tempuser = $db->fetch_array($query))
	{
		$userlist[$tempuser['id']] = $tempuser;
	}
}

if (empty($action))
{
	if (submitcheck('reply'))
	{
		if (!trim($replytext))
		{
			$db->query("delete from `{$tablepre}record` where `id`='{$inputcid}'");
			showmessage($lang['clarification_ignore'], 'admin.php');
		}
		else
		{
			$db->query("update `{$tablepre}record` set `type`='notice', `event`='{$replytext}' where `id`={$inputcid}");
			showmessage($lang['clarification_reply'], 'admin.php');
		}
	}
	$clarifications = array();
	$query = $db->query("select * from `{$tablepre}record` where `type`='clarify'");
	while ($tempclarification = $db->fetch_array($query))
	{
		$clarifications[$tempclarification['id']] = $tempclarification;
	}
}

//Generating Menu
$menulist[1]['flag'] = true;
$menulist[1]['title'] = $lang['text']['settings'];
$menulist[1]['items'][1]['text'] = $lang['link']['admin_homepage'];
$menulist[1]['items'][1]['url'] = "admin.php";
$menulist[1]['items'][2]['text'] = $lang['link']['admin_settings'];
$menulist[1]['items'][2]['url'] = "admin.php?action=settings";
$menulist[1]['items'][3]['text'] = $lang['link']['admin_judge'];
$menulist[1]['items'][3]['url'] = "admin.php?action=judge";
$menulist[1]['items'][4]['text'] = $lang['link']['admin_add'];
$menulist[1]['items'][4]['url'] = "admin.php?action=add";
$menulist[1]['items'][5]['text'] = $lang['link']['admin_list'];
$menulist[1]['items'][5]['url'] = "admin.php?action=list";
$menulist[1]['items'][6]['text'] = $lang['link']['admin_notice'];
$menulist[1]['items'][6]['url'] = "admin.php?action=announce";
$menulist[1]['items'][7]['text'] = $lang['link']['admin_userlist'];
$menulist[1]['items'][7]['url'] = "admin.php?action=userlist";
//Generating Menu End

include template('admin');

?>